/*
 * Created on Jul 17, 2005
 *
 */
package org.mushroomdb.interceptor;

import java.util.Iterator;

import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.EncodeableRegister;
import org.mushroomdb.catalog.Register;
import org.mushroomdb.catalog.Table;
import org.mushroomdb.catalog.WriteBackRegister;
import org.mushroomdb.filemanagement.Page;


/**
 * @author Matu
 *
 */
public class InterceptedRegister implements EncodeableRegister, WriteBackRegister {
	
	private Interceptor interceptor;
	private Register register;
	private Table table;

	/**
	 * Constructor default
	 */
	public InterceptedRegister(Interceptor interceptor, Register register, Table table) {
		this.interceptor = interceptor;
		this.register = register;
		this.table = table;
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumns()
	 */
	public Iterator getColumns() {
		return this.register.getColumns();
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnIndex(java.lang.String)
	 */
	public int getColumnIndex(Column column) {
		return this.register.getColumnIndex(column);
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getValue(int)
	 */
	public Object getValue(int columnIndex) {
		return this.register.getValue(columnIndex);
	}

	/**
	 * @see org.mushroomdb.catalog.Register#setValue(int, java.lang.Object)
	 */
	public void setValue(int columnIndex, Object value) {
		this.interceptor.beforeUpdate(this.register,columnIndex,value,this.table);
		this.register.setValue(columnIndex,value);
		this.interceptor.afterUpdate(this.register,columnIndex,value,this.table);
	}

	/**
	 * @see org.mushroomdb.catalog.Register#getColumnCount()
	 */
	public int getColumnCount() {
		return this.register.getColumnCount();
	}

	/**
	 * toString
	 */
	public String toString() {
		int columnCount = this.getColumnCount();
		String rv = ""; //$NON-NLS-1$
		
		for (int i = 1; i <= columnCount; i++) {
			if (rv.length() > 0) {
				rv = rv + ',';
			}
			
			Object object = this.getValue(i).toString();
			if (object instanceof String) {
				rv = rv + '\'' + object.toString() + '\'';
			} else {
				rv = rv + object.toString();
			}
		}
		
		return '(' + rv + ')';
	}

	/**
	 * @see org.mushroomdb.catalog.Register#remove()
	 */
	public void remove() {
		this.interceptor.beforeDelete(this.register,this.table);
		this.register.remove();
		this.interceptor.afterDelete(this.register,this.table);
	}

	public byte[] toByteArray() {
		if (this.register instanceof EncodeableRegister) {
			return ((EncodeableRegister)this.register).toByteArray();
		}
		return null;
	}

	public int getOffset() {
		if (this.register instanceof WriteBackRegister) {
			return ((WriteBackRegister)this.register).getOffset();
		}
		return 0;
	}

	public Page getPage() {
		if (this.register instanceof WriteBackRegister) {
			return ((WriteBackRegister)this.register).getPage();
		}
		return null;
	}

	public boolean isInTrx() {
		if (this.register instanceof WriteBackRegister) {
			return ((WriteBackRegister)this.register).isInTrx();
		}
		return false;
	}

	public void setOffset(int offset) {
		if (this.register instanceof WriteBackRegister) {
			((WriteBackRegister)this.register).setOffset(offset);
		}
	}

	public void setPage(Page page) {
		if (this.register instanceof WriteBackRegister) {
			((WriteBackRegister)this.register).setPage(page);
		}
	}
}
